using System;
using System.Text.RegularExpressions;


namespace Ultima {
  public class StringEntry {
    private static readonly object[] m_Args = new object[] { "", "", "", "", "", "", "", "", "", "", "" };
    private static readonly Regex m_RegEx = new Regex( @"~(\d+)[_\w]+~",
                                                      RegexOptions.Compiled | RegexOptions.IgnoreCase |
                                                      RegexOptions.Singleline | RegexOptions.CultureInvariant );
    private readonly int m_Number;
    private readonly string m_Text;
    private string m_FmtTxt;

    public StringEntry( int number, string text ) {
      this.m_Number = number;
      this.m_Text = text;
      this.m_FmtTxt = null;
    }

    public int Number {
      get { return this.m_Number; }
    }
    public string Text {
      get { return this.m_Text; }
    }

    public string Format( params object[] args ) {
      if ( this.m_FmtTxt == null )
        this.m_FmtTxt = m_RegEx.Replace( this.m_Text, @"{$1}" );
      for ( int i = 0; i < args.Length && i < 10; i++ )
        m_Args[i + 1] = args[i];
      return String.Format( this.m_FmtTxt, m_Args );
    }

    public string SplitFormat( string argstr ) {
      if ( this.m_FmtTxt == null )
        this.m_FmtTxt = m_RegEx.Replace( this.m_Text, @"{$1}" );
      string[] args = argstr.Split( '\t' ); // adds an extra on to the args array
      for ( int i = 0; i < args.Length && i < 10; i++ )
        m_Args[i + 1] = args[i];
      return String.Format( this.m_FmtTxt, m_Args );
      /*
			{
				StringBuilder sb = new StringBuilder();
				sb.Append( m_FmtTxt );
				for(int i=0;i<args.Length;i++)
				{
					sb.Append( "|" );
					sb.Append( args[i] == null ? "-null-" : args[i] );
				}
				throw new Exception( sb.ToString() );
			}*/
    }
  }
}